package com.bawei.lib_common.base

import android.app.Application
import cn.jiguang.api.utils.JCollectionAuth
import cn.jpush.android.api.JPushInterface
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_common.flow_eventbus.EventBusInitializer
import com.tencent.matrix.Matrix
import com.tencent.matrix.resource.ResourcePlugin
import com.tencent.matrix.resource.config.ResourceConfig
import com.tencent.matrix.trace.TracePlugin
import com.tencent.matrix.trace.config.TraceConfig
import com.tencent.mmkv.MMKV
import com.tencent.mrs.plugin.IDynamicConfig


/**
 *作者：lxy
 *功能:
 *时间：2025/5/22
 */
open class BaseApplication:Application() {

    companion object{
        lateinit var instance:BaseApplication
    }
    override fun onCreate() {
        super.onCreate()
        instance = this
        initARouter()
        initMMKV()
        initAppHolder()
        initJPush()
        initMatrix()
    }

    private fun initMatrix() {

        val builder: Matrix.Builder = Matrix.Builder(this)
        // 2.1 配置卡顿监控插件
        val traceConfig: TraceConfig = TraceConfig.Builder()
            .enableAnrTrace(true) // 监控ANR
            .enableEvilMethodTrace(true) // 监控耗时方法
            .setTouchEventThreshold(500)// 阈值500ms
            .build()

        builder.plugin(TracePlugin(traceConfig))



        // 2.2 配置内存泄漏监控插件
        val resourceConfig: ResourceConfig = ResourceConfig.Builder()
            .dynamicConfig(MatrixDynamicConfig())
            .setDetectDebuger(true) // 开启泄漏检测
            .build()

        builder.plugin(ResourcePlugin(resourceConfig))



        // 3. 初始化 Matrix
        Matrix.init(builder.build())


        // 4. 启动所有插件
        Matrix.with().startAllPlugins()
    }

    private fun initJPush() {
        JCollectionAuth.setAuth(this,true)
        JPushInterface.setDebugMode(true)//BuildConfig.Debug
        JPushInterface.init(this)
    }

    private fun initAppHolder() {

    }

    private fun initMMKV() {
        MMKV.initialize(this)
    }

    private fun initARouter() {
        ARouter.openLog()
        ARouter.openDebug()
        ARouter.init(this)
        EventBusInitializer.init(this)
    }
}


class MatrixDynamicConfig : IDynamicConfig {
    override fun get(key: String?, defValue: String?): String? {
        return when (key) {
            "BACKGROUND_SCAN_INTERVAL" -> "30000" // 设置扫描间隔30秒
            else -> defValue
        }
    }

    override fun get(key: String?, defValue: Int): Int {
        // 示例：ANR超时时间
        if (key == "KEY_ANR_THRESHOLD") return 5000
        return defValue
    }

    override fun get(key: String?, defValue: Long): Long {
        // 关键配置：解决当前问题的内存扫描间隔
        if (key == "BACKGROUND_SCAN_INTERVAL") return 30000 // 30秒

        // 其他配置
        return when (key) {
            "EVIL_METHOD_THRESHOLD" -> 800 // 耗时方法阈值
            "MEMORY_CHECK_INTERVAL" -> 60000 // 内存检查间隔
            else -> defValue
        }
    }

    override fun get(key: String?, defValue: Boolean): Boolean {
        return when (key) {
            "ENABLE_RESOURCE_DETECT" -> true // 确保开启资源检测
            else -> defValue
        }
    }

    override fun get(key: String?, defValue: Float): Float = defValue
}